package imoocvideodev.service.impl;

import imoocvideodev.common.util.MD5Util;
import imoocvideodev.common.util.UUIDUtils;
import imoocvideodev.dao.UsersFansMapper;
import imoocvideodev.dao.UsersLikeVideosMapper;
import imoocvideodev.dao.UsersMapper;
import imoocvideodev.dao.UsersReportMapper;
import imoocvideodev.pojo.Users;
import imoocvideodev.pojo.UsersFans;
import imoocvideodev.pojo.UsersLikeVideos;
import imoocvideodev.pojo.UsersReport;
import imoocvideodev.service.UsersService;
import imoocvideodev.common.result.IMoocJSONResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @Author: Redi
 * @Date: 2021/1/31 12:05
 * @Description: 用户服务实现类
 */
@Service
public class UsersServiceImpl implements UsersService {

    @Autowired
    UsersMapper usersMapper;
    @Autowired
    UsersLikeVideosMapper usersLikeVideosMapper;
    @Autowired
    UsersFansMapper usersFansMapper;
    @Autowired
    UsersReportMapper usersReportMapper;

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public boolean checkUserExist(String username) {
        if((usersMapper.selectByUsername(username))!=null){
            return true;
        }
        return false;
    }

    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public IMoocJSONResult saveUser(Users users) {

        int resultCount = usersMapper.insert(users);
        if (resultCount>0){
            return IMoocJSONResult.build(200,"用户创建成功。",users);
        }
        return IMoocJSONResult.errorMsg("用户创建失败");
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public Users login(Users users) throws Exception {
        Users selectUser = usersMapper.selectLogin(users.getUsername(),MD5Util.MD5EncodeUtf8(users.getPassword()));
            //测试卡顿现象
//            Thread.sleep(3000);
        return selectUser;
    }

    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public IMoocJSONResult update(Users users){
        int resultCount = usersMapper.updateByPrimaryKeySelective(users);
        if (resultCount > 0){
            return IMoocJSONResult.okMsg("用户信息更新成功！");
        }
        return IMoocJSONResult.errorMsg("用户信息更新失败!");
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public Users selectUsersById(String usersId){
        return usersMapper.selectByPrimaryKey(usersId);
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public Boolean isUserLikeVideo(String loginUserId, String videoId) {
        UsersLikeVideos record = usersLikeVideosMapper.selectByUserIdAndVideoId(loginUserId, videoId);
        if (record != null){
            return true;
        }
        return false;
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public Boolean checkIfFollow(String userId, String fanId) {
        UsersFans usersFans = usersFansMapper.selectByUserIdAndFanId(userId, fanId);
        if (usersFans != null){
            return true;
        }
        return false;
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public IMoocJSONResult beFans(String userId, String fanId) {
        UsersFans usersFans = new UsersFans();
        usersFans.setId(UUIDUtils.getRandomUUID());
        usersFans.setUserId(userId);
        usersFans.setFanId(fanId);
        int resultCount = usersFansMapper.insert(usersFans);
        if (resultCount > 0){
            return IMoocJSONResult.build(200,"关注成功！",usersFans);
        }
        else{
            return IMoocJSONResult.errorMsg("关注失败");
        }
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public IMoocJSONResult dontBeFans(String userId, String fanId) {
        UsersFans usersFans = usersFansMapper.selectByUserIdAndFanId(userId, fanId);
        int resultCount = usersFansMapper.deleteByPrimaryKey(usersFans.getId());

        if (resultCount > 0){
            return IMoocJSONResult.okMsg("取关成功！");
        }
        else{
            return IMoocJSONResult.errorMsg("取关失败");
        }
    }

    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public IMoocJSONResult reportUsers(UsersReport usersReport) {
        usersReport.setId(UUIDUtils.getRandomUUID());
        usersReport.setCreateDate(new Date());
        int resultCount = usersReportMapper.insert(usersReport);
        if (resultCount > 0){
            return IMoocJSONResult.okData(usersReport);
        }
        return IMoocJSONResult.errorMsg("举报信息发送失败...");
    }
}
